﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// Room Controller
    /// </summary>
    public class RoomController : Controller
    {
        private const int DEFAULTPAGESIZE = 1;
        readonly IRoomRepository _roomRepository;

        /// <summary>
        /// Dependency Injection enabled constructors
        /// </summary>
        public RoomController() : this(new RoomRepository()) { }

        /// <summary>
        /// Room Controller Constructor
        /// </summary>
        /// <param name="roomRepository"></param>
        public RoomController(IRoomRepository roomRepository)
        {
            _roomRepository = roomRepository;
        }

        /// <summary>
        /// Display the listing of room
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string strQuery, int? page)
        {
            IQueryable<Room> rooms = null;
            rooms = !string.IsNullOrWhiteSpace(strQuery) ? _roomRepository.FindRoomByName(strQuery).OrderBy(d => d.CreatedDate) : _roomRepository.FindAllRooms().OrderBy(d => d.CreatedDate);
            var paginatedRoom = rooms.ToPagedList(page.HasValue ? page.Value - 1 : 0, DEFAULTPAGESIZE);
            return View(paginatedRoom);
        }

        /// <summary>
        /// Details of Room
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int? id)
        {
            if (id != null)
            {
                var room = _roomRepository.GetRoomById(id.Value);

                if (room == null)
                {
                    return new FileNotFoundResult { Message = "No Room found for that id" };
                }

                return View(room);
            }

            return new FileNotFoundResult { Message = "No Room found due to invalid room id" };
        }

        /// <summary>
        /// Create Room
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create()
        {
            var room = new Room();
            return View(new RoomViewModel(room));
        }

        /// <summary>
        /// Create Room
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(Room room)
        {
            if (ModelState.IsValid)
            {
                room.CreatedDate = DateTime.Now.ToSqlDateTime();
                room.IsActive = Convert.ToBoolean(Request["IsActive"]);
                _roomRepository.Add(room);
                _roomRepository.Save();
                return RedirectToAction("Details", new { id = room.RoomId });
            }

            return View(new RoomViewModel(room));
        }

        /// <summary>
        /// Edit Room
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            var room = _roomRepository.GetRoomById(id);
            return View(new RoomViewModel(room));
        }

        /// <summary>
        /// Edit Notification Email
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var room = _roomRepository.GetRoomById(id);
            if (ModelState.IsValid)
            {
                try
                {
                    room.ModifiedDate = DateTime.Now;
                    UpdateModel(room, "Room");
                    _roomRepository.Save();
                    return RedirectToAction("Details", new { id = room.RoomId });
                }
                catch
                {
                    return View(new RoomViewModel(room));
                }
            }
            return View(new RoomViewModel(room));
        }

        /// <summary>
        /// Delete Room
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            var room = _roomRepository.GetRoomById(id);
            return room == null ? View("NotFound") : View(room);
        }

        /// <summary>
        /// Delete Room
        /// </summary>
        /// <param name="id"></param>
        /// <param name="confirmButton"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Delete(int id, string confirmButton)
        {
            var room = _roomRepository.GetRoomById(id);

            if (room != null)
            {
                _roomRepository.Delete(room);
                _roomRepository.Save();
                return View("Delete");
            }
            return View("NotFound");
        }

        /// <summary>
        /// Handle Unknown Action
        /// </summary>
        /// <param name="actionName"></param>
        /// <exception cref="HttpException"></exception>
        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action not found");
        }
    }
}